package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.IsDeletdFlageEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoEnum;
import com.jingdianjichi.subject.domain.convert.SubjectJudgeConverter;
import com.jingdianjichi.subject.domain.convert.SubjectMultipleConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectJudge;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMultiple;
import com.jingdianjichi.subject.infra.basic.service.SubjectMultipleService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * SubjectRadioHandler 类
 *
 * @author 熊苏鹏1
 * @date 2025/2/20 21:06
 * @description 单选策略
 */
@Component
public class SubjectMultipleHandler implements SubjectTypeHandler{
    @Resource
    SubjectMultipleService subjectMultipleService;
    @Override
    public SubjectInfoEnum getSubjectType() {
        return SubjectInfoEnum.MULITIPLE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectMultiple> subjectRadioList = new ArrayList<>();
        List<SubjectOptionBO> subjectOptionList = subjectInfoBO.getSubjectOptionList();
        subjectOptionList.forEach(option -> {
            SubjectMultiple subjectMultiple = SubjectMultipleConverter.INSTANCE.convertBoToEntity(option);
            subjectMultiple.setSubjectId(subjectInfoBO.getId());
            subjectMultiple.setIsDeleted(IsDeletdFlageEnum.NOT_DELETED.getCode());
            subjectRadioList.add(subjectMultiple);
        });
        subjectMultipleService.insertBatch(subjectRadioList);

    }

    @Override
    public SubjectAnswerBO query(SubjectInfoBO subjectInfoBO) {
        SubjectMultiple subjectMultiple = new SubjectMultiple();
        subjectMultiple.setSubjectId(subjectInfoBO.getId());
        List<SubjectMultiple> subjectMultiples = subjectMultipleService.query(subjectMultiple);
        List<SubjectOptionBO> subjectOptionBOList = SubjectMultipleConverter.INSTANCE.convertEntityToBoList(subjectMultiples);
        SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
        subjectAnswerBO.setSubjectOptionList(subjectOptionBOList);
        return subjectAnswerBO;
    }
}
